The given array is a = {7,8,9,6,5}. The number of iterations in select...
The selection sort algorithm sorts an array by repeatedly finding the minimum element from the unsorted part and putting it at the beginning.So, Every pass 1 swap minimum is required.
The selection sort is good for swapping purposes.
According to the question , the array is {7,8,9,6,5}
Applying selection sort :
1st pass - 5 {7,8,9,6}
2nd pass - 5, 6 {7,8,9}
3rd pass - 5, 6, 7 {8,9}
4th pass - 5,6,7,8 {9} ( array sorted)
So, we need 4 passes/iterations to sort the array using selection sort.
Bubble sort work by the principle of finding the max element from the array and pushing it to the end.
Applying bubble sort on {7,8,9,6,5}
1st pass- {7,8,6,5} 9
2nd pass - {7,6,5} 8, 9
3rd pass- {6,5} 7, 8, 9
4th pass - {5} 6, 7, 8, 9 ( array sorted )
So, we need 4 passes to sort the array using bubble sort.
Both selection and bubble sort takes (n-1) passes / iteration to sort the array.
The given array is a = {7,8,9,6,5}. The number of iterations in select...
Selection Sort and Bubble Sort
Selection Sort:
- Selection Sort is a simple comparison-based sorting algorithm.
- It works by dividing the array into two parts - sorted and unsorted.
- In each iteration, the smallest element from the unsorted part is selected and placed at the beginning of the sorted part.
- This process continues until the entire array is sorted.
Bubble Sort:
- Bubble Sort is another simple comparison-based sorting algorithm.
- It works by repeatedly swapping adjacent elements if they are in the wrong order.
- In each iteration, the largest element in the unsorted part "bubbles up" to its correct position.
- This process continues until the entire array is sorted.
Given Array:
a = {7, 8, 9, 6, 5}
Selection Sort iterations:
- In the first iteration, 5 (the smallest element) is selected and placed at the beginning. The array becomes: {5, 8, 9, 6, 7}
- In the second iteration, 6 (the next smallest element) is selected and placed after 5. The array becomes: {5, 6, 9, 8, 7}
- In the third iteration, 7 is selected and placed after 6. The array becomes: {5, 6, 7, 8, 9}
- In the fourth iteration, 8 is selected and placed after 7. The array remains the same: {5, 6, 7, 8, 9}
Bubble Sort iterations:
- In the first iteration, 7 and 8 are compared. Since 8 is greater, they are swapped. The array becomes: {8, 7, 9, 6, 5}
- In the second iteration, 8 and 9 are compared. Since they are in the correct order, no swap occurs. The array remains the same: {8, 7, 9, 6, 5}
- In the third iteration, 9 and 6 are compared. Since 9 is greater, they are swapped. The array becomes: {8, 7, 6, 9, 5}
- In the fourth iteration, 9 and 5 are compared. Since 9 is greater, they are swapped. The array becomes: {8, 7, 6, 5, 9}
Conclusion:
- The number of iterations in the selection sort is 4, while the number of iterations in the bubble sort is 4.
- Therefore, the correct answer is option 'A', i.e., 4 iterations for selection sort and 4 iterations for bubble sort.